//
//  PopNumberView.m
//  DaleTou
//
//  Created by candyxj on 2018/5/28.
//  Copyright © 2018年 geren. All rights reserved.
//

#import "PopNumberView.h"
#import "UIView+CustomAlertView.h"

@interface PopNumberView ()
@property (nonatomic, strong) NSArray *numArray;
@end

@implementation PopNumberView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {        
        self.backgroundColor = [UIColor clearColor];
        //        self.layer.cornerRadius = 5;
        //        self.layer.masksToBounds = YES;
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 60)];
        topView.backgroundColor = [UIColor colorWithRed:255.0/255 green:242.0/255 blue:197.0/255 alpha:1.0];
        topView.layer.cornerRadius = 10;
        topView.layer.masksToBounds = YES;
        [self addSubview:topView];
        
        UILabel *titleLb = [[UILabel alloc] initWithFrame:CGRectMake( 15, 30, self.frame.size.width - 30, 20)];
        titleLb.text = @"恭喜您获得一注幸运数字:";
        titleLb.textColor = [UIColor blackColor];
        titleLb.font = [UIFont systemFontOfSize:16];
        titleLb.textAlignment = NSTextAlignmentCenter;
        [self addSubview:titleLb];
        
        //获取随机数
        _numArray = [self getRadomNum];
        CGFloat W = 30;
        CGFloat H = W;
        CGFloat pad = (self.frame.size.width - 20 - W * 7) / 8;
        for (int i = 0; i < 7; i ++) {
            
            UIButton *btn = [[UIButton alloc] init];
            btn.titleLabel.font = [UIFont systemFontOfSize:14];
            btn.adjustsImageWhenHighlighted = NO;
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            btn.layer.cornerRadius = W / 2;
            btn.layer.masksToBounds = YES;
            [btn setTitle:[NSString stringWithFormat:@"%@ ",_numArray[i]] forState:UIControlStateNormal];
            btn.frame = CGRectMake( 10 + (pad + W)* (i + 0), CGRectGetMaxY(titleLb.frame) + 30, W, H);
            btn.tag = i;
            if ( i <= 4) {
                btn.backgroundColor = [UIColor redColor];
            }else{
                btn.backgroundColor = [UIColor blueColor];
            }
            
            [self addSubview:btn];
        }
        
        UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [commitBtn setTitle:@"立即投注" forState:UIControlStateNormal];
        commitBtn.layer.cornerRadius = 10;
        commitBtn.layer.masksToBounds = YES;
        commitBtn.frame = CGRectMake(CGRectGetMidX(self.frame) - 60, CGRectGetMaxY(titleLb.frame) + 50 + 30, 120, 30);
        commitBtn.backgroundColor = [UIColor colorWithRed:252.0/255 green:100.0/255 blue:32.0/255 alpha:1.0];
        [commitBtn addTarget:self action:@selector(commitBtnClicked) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:commitBtn];        
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        closeBtn.frame = CGRectMake(CGRectGetMidX(topView.frame) - 15, CGRectGetMaxY(topView.frame) + 30, 30, 30);
        [closeBtn setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
//        closeBtn.backgroundColor = [UIColor purpleColor];
        [closeBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:closeBtn];
        
    }
    return self;
}

- (void)commitBtnClicked{
    [self jk_hideView];
    if (self.commitBtnClickedBlock) {
        self.commitBtnClickedBlock(self.numArray);
    }
}

- (void)btnClick:(UIButton *)btn{
    [self jk_hideView];
}



- (NSMutableArray *)getRadomNum{
    NSMutableArray *readArray = [NSMutableArray array];
    NSMutableArray *blueArray = [NSMutableArray array];
    while (readArray.count<5) {
        int x = arc4random()%35 +1;
        NSNumber *number = [NSNumber numberWithInteger:x];
        if (![readArray containsObject:number]) {
            [readArray addObject:number];
        }
    }
    
    while (blueArray.count<2) {
        int x = arc4random()%12 +1;
        NSNumber *number = [NSNumber numberWithInteger:x];
        if (![blueArray containsObject:number]) {
            [blueArray addObject:number];
        }
    }
    
    NSArray *numArray = [readArray arrayByAddingObjectsFromArray:blueArray];
    return numArray;
}



@end
